package com.vhall.component.service.room.event.handler.record;

import com.vhall.component.service.room.event.handler.AbstractEventHandler;
import com.vhall.component.service.room.event.handler.EventHandlerType;
import com.vhall.component.service.room.event.handler.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

import static com.vhall.component.plugin.common.constant.RedisKey.FIRST_FRAME_FROM_VIDEO;
import static com.vhall.component.service.room.event.EventConstant.VOD_COVER_COMPLETE;

/**
 * @author shimingming
 * @since 2023/5/24 16:54
 */
@Slf4j
@Component
@EventHandlerType(VOD_COVER_COMPLETE)
public class VODCoverCompleteEventHandler extends AbstractEventHandler {

    @Resource
    public RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private RedirectUtil redirectUtil;
    @Value("${old.callback.record.url}")
    private String callbackUrl;
    @Override
    public void handle(Map<String, Object> callBack) {
//        redirectUtil.redirect(callbackUrl, callBack);
        log.info("生成媒体封面完成：{}", callBack);

        // {errno=200, signature=b6b51eb65f7a3c8cc5cd96700bf0a10e, vod_id=c1823755, task_id=a7dc5c64-f782-4f1e-98a2-06c3f2865029,
        // repeat_times=3, datetime=2023-05-25 10:03:34, refer=vhall, err_msg=,
        // vod_cover_url=https://static01-open.e.vhall.com/vhallyun/vhallimage/vodcover/c1823755/cover.jpg, time=1684980214,
        // event=VODCoverComplete, app_id=TVB9THkQ, status=1}

        if (callBack.get("errno") != null
                && "200".equals(callBack.get("errno").toString())
                && callBack.get("vod_id") != null
                && callBack.get("vod_cover_url") != null
                && callBack.get("status") != null
                && "1".equals(callBack.get("status").toString())) {

            String key = FIRST_FRAME_FROM_VIDEO + callBack.get("vod_id").toString();
            redisTemplate.opsForValue().set(key, callBack.get("vod_cover_url").toString());
        }


    }


}
